﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Xml;

public partial class Services_getRSIndex_Drought : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

        string imageId = Request.QueryString[0].ToString();
        int blockSize = Convert.ToInt32(Request.QueryString[1].ToString().Remove(0, 4));


        string imageType = "multi";
        string result = "";
        string SP_File_Name = "";
        string Id = "";//行政区划编号,09-4-3添加，ReSamplePointRemoteSensingIndex动态链接库更新后的方法中需要这个参数
        DateTime ImageDate = new DateTime();


        string resultXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
 //     resultXml += "<ImageList>" + "\n";

        try
        {
            string queryString = "select SP_FILE_NAME,IMAGEDATE,DISTID from DISTRICT_IMAGE_SP_MAP where imageid like '%" + imageId + "%'";
            DataSet ds = AccessDatabase.Query(queryString);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count == 1)
            {
                SP_File_Name = dt.Rows[0][0].ToString();

                ImageDate = Convert.ToDateTime(dt.Rows[0][1].ToString().Substring(0, 8));

                Id = dt.Rows[0][2].ToString();
            }
            else
            {
                resultXml += "<url>Error! Can't find your request image on server,please check again!</url>";
            }
        }
        catch (Exception ex)
        {
            resultXml += "<url>Error!" + ex.Message.ToString() + "</url>";
        }

        string baseLocation1 = Server.MapPath("../");
        DroughtIndexCalculator dicl = new DroughtIndexCalculator();
        result = dicl.CalculateDroughtIndexToXmlDoc(SP_File_Name, imageId, baseLocation1 + "data\\", blockSize, Id, ImageDate,imageType);

        Regex pathRegex = new Regex(@"([a-zA-Z]:)*\w+\.xml");//判断result是否是以.xml结尾的路径字符串的正则表达式
        bool bool_result = pathRegex.IsMatch(result);
        if (result != "0" && bool_result)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(result);
            XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName("Parcel");
            int nChildren = xmlNodeList.Count;
            int nAttributes = xmlNodeList[0].ChildNodes.Count;

            resultXml += "<" + xmlDoc.ChildNodes[0].Name + ">" + "\n";
            for (int i = 0; i < nChildren; i++)
            {
                resultXml += "<" + xmlNodeList[i].Name + " ";
                for (int j = 0; j < nAttributes; j++)
                {
                    resultXml += xmlNodeList[i].ChildNodes[j].Name + "='" + xmlNodeList[i].ChildNodes[j].FirstChild.Value + "' ";
                }
                resultXml += " />" + "\n";
            }
            resultXml += "</" + xmlDoc.ChildNodes[0].Name + ">" + "\n";
        }
        else
        {
            resultXml += "<url>Error!Calculate Error!</url>";
        }

        Response.Write(resultXml); 
    }
}
